﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using MasterDetailTemplate.Services;

namespace MasterDetailTemplate.ViewModels {
    public class MainPageViewModel : ViewModelBase {
        /// <summary>
        /// 事件存储。
        /// </summary>
        private IEventStorage _eventStorage;

        /// <summary>
        /// 根导航。
        /// </summary>
        private IRootNavigationService _rootNavigationService;

        /// <summary>
        /// MainPage viewmodel。
        /// </summary>
        /// <param name="eventStorage">事件存储。</param>
        /// <param name="rootNavigationService">根导航。</param>
        public MainPageViewModel(IEventStorage eventStorage,
            IRootNavigationService rootNavigationService) {
            _eventStorage = eventStorage;
            _rootNavigationService = rootNavigationService;
        }

        /// <summary>
        /// 页面显示命令。
        /// </summary>
        private RelayCommand _pageAppearingCommand;

        /// <summary>
        /// 页面显示命令
        /// </summary>
        public RelayCommand PageAppearingCommand =>
            _pageAppearingCommand ?? (_pageAppearingCommand =
                new RelayCommand(async () => {
                    await PageAppearingCommandFunction();
                }));

        public async Task PageAppearingCommandFunction() {
            if (!_eventStorage.Initialized())
                await _rootNavigationService.NavigateToAsync(
                    RootNavigationServiceConstants.InitializationPage);
        }
    }
}